﻿function findParentElement(el,elementID){ // 判断'el'是不是属于'elementID'，'elementID'是标签的id值

  newel = el;
  while(newel.id.toLowerCase()!=elementID && newel.parentNode){
    newel = newel.parentNode;
	if(newel.tagName.toLowerCase() == 'html') break;
  }
  if(newel.id.toLowerCase() == elementID ) return true
  else return false
}
function findElement(el,element){ // 寻找tagName=element的el父对象，'element'是标签名
  newel = el;
  while(newel.tagName.toLowerCase()!=element && newel.parentNode){
    newel = newel.parentNode;
  }
  if(newel.tagName.toLowerCase() == element) return newel;
  else return false;
}


var gdCtrl = new Object();
var gcGray   = "#cccccc";
var gcToggle = "#f2f6f9"; //#e5eaef
var gcBG = "#ffffff";
var previousObject = null;

var gdCurDate = new Date();
var cYear = gdCurDate.getFullYear();
var cMonth = gdCurDate.getMonth()+1;
var cDay = gdCurDate.getDate();
var giYear = gdCurDate.getFullYear();
var giMonth = gdCurDate.getMonth()+1;
var giDay = gdCurDate.getDate();

var closeFlag = 0;
var blnFlag = true;


// function isValidDate() added by weng.jianhuan at 2004-04-30   判断两个日期的大小

// 输入框命名为：name=start,name=end
// 当页面上有多对发布起始、到期日输入框时，输入框命名为：name=start_1,name=end_1,name=start_2,name=end_2......字母和数字必须用下划线'_'分开
var errorText = '错误的日期！';
function isValidDate(whichDate,y,m,d){
  nameStr = whichDate.name;
  nameArr = nameStr.split('_');
  if(nameArr[0] == 'start'){
     errorText = '起始日不能大于结束日！';
	 if(parForm = findElement(whichDate,'form')){ 
         if(nameArr.length > 1) objEnd = parForm.elements['end_'+nameArr[1]];
	     else objEnd = parForm.elements['end'];
	 }
	 else{
	        if(nameArr.length > 1) objEnd = eval("document.getElementById('end_" + nameArr[1] + "')");
	        else objEnd = eval("document.getElementById('end')");
	 }
	 if(objEnd.value == '') return true;
	 else { var dateEnd = objEnd.value;
	         dateArr = dateEnd.split('-');
			if((y*10000+m*100+d*1)>(dateArr[0]*10000+dateArr[1]*100+dateArr[2]*1)) return false;}}
			//var tDate = new Date();
			//if(tDate.setFullYear(y,m,d)>tDate.setFullYear(dateArr[0],dateArr[1],dateArr[2])) return false;}}
  else if(nameArr[0] == 'end'){
     errorText = '结束日不能小于起始日！';
     if(parForm = findElement(whichDate,'form')){ 
         if(nameArr.length > 1) objStart = parForm.elements['start_'+nameArr[1]];
	     else objStart = parForm.elements['start'];
	 }
	 else{
	        if(nameArr.length > 1) objStart = eval("document.getElementById('start_" + nameArr[1] + "')");
	        else objStart = eval("document.getElementById('start')");
	 }
	 if(objStart.value == '') return true;
	 else { var dateStart = objStart.value;
	         dateArr = dateStart.split('-');
			if((y*10000+m*100+d*1)<(dateArr[0]*10000+dateArr[1]*100+dateArr[2]*1)) return false;}}
			//var tDate = new Date();
			//if(tDate.setFullYear(y,m,d)<tDate.setFullYear(dateArr[0],dateArr[1],dateArr[2])) return false;}}
  return true;
}

/********************************************* pass strDate to 'dateCtrl' *********************************************/
var functionAfterSetDate = null;
function fSetDate(iYear,iMonth,iDay){
  //VicPopCal.style.visibility = "hidden";
  var flagCloseDiv = true;
  if ((iYear == 0) && (iMonth == 0) && (iDay == 0)){
  	gdCtrl.value = "";
  }else{
  	iMonth = iMonth + 100 + "";
  	iMonth = iMonth.substring(1);
  	iDay   = iDay + 100 + "";
  	iDay   = iDay.substring(1);
  	if(gdCtrl.tagName == "INPUT"){
	   // 发布其始日 和 发布终止日 （调用isValidDate()判断两个日期的大小） added by weng.jianhuan
	   if(gdCtrl.name.indexOf('start') > -1 || gdCtrl.name.indexOf('end') > -1){
  	    if(isValidDate(gdCtrl,iYear, iMonth, iDay)) gdCtrl.value = iYear+"-"+iMonth+"-"+iDay;
		else { flagCloseDiv = false;fnShowError(errorText);return false;}}
	   //单个日期输入 (输入框name值里不能含有"start"和"end")
	   else gdCtrl.value = iYear+"-"+iMonth+"-"+iDay;
  	}else{
  	  // 发布其始日 和 发布终止日 （调用isValidDate()判断两个日期的大小）  added by weng.jianhuan
	  if(gdCtrl.name.indexOf('start') > -1 || gdCtrl.name.indexOf('end') > -1){
	    if(isValidDate(gdCtrl,iYear, iMonth, iDay))	gdCtrl.innerText = iYear+"-"+iMonth+"-"+iDay;
	    else { flagCloseDiv = false;fnShowError(errorText);return false;}}
	  //单个日期输入 (输入框name值里不能含有"start"和"end")
	  else gdCtrl.value = iYear+"-"+iMonth+"-"+iDay;
  	}
  }
  
  if(functionAfterSetDate!=null && typeof functionAfterSetDate =='function') functionAfterSetDate();//edited by weng 2005.10.26
  
  if(flagCloseDiv) HiddenDiv();
  
  window.returnValue=gdCtrl.value;
  //window.close();
}
function fSetSelected(aCell){
  var iOffset = 0;
  var iYear = parseInt(tbSelYear.value);
  var iMonth = parseInt(tbSelMonth.value);
  
  aCell.bgColor = gcBG;
  with (aCell.childNodes[0]){
  	var iDay = parseInt(innerHTML);
  	//if (color==gcGray) iOffset = (Victor<10)?-1:1;
  	if(getAttribute('flagMonth')==0) iOffset = (getAttribute('Victor')<10)?-1:1;
	iMonth += iOffset;
	if (iMonth<1) {
		iYear--;
		iMonth = 12;
	}else if (iMonth>12){
		iYear++;
		iMonth = 1;
	}
  }
  fSetDate(iYear, iMonth, iDay);
}
/********************************************* pass strDate to 'dateCtrl' *********************************************/

function closeCalendar(setType){
  if(setType == "on"){
    if(closeFlag != 0){
      clearTimeout(closeFlag);
      closeFlag = 0;
    }
  }else{
    if(VicPopCal.style.visibility == "visible"){
      closeFlag = setTimeout("HiddenDiv2()",1000);
      blnFlag = true;
    }
  }
}

function HiddenDiv(){	
	var i;
    VicPopCal.style.visibility = "hidden";
    if(VicPopCal.arrToggleTags){
       for (i=0;VicPopCal.arrToggleTags[i];i++) VicPopCal.arrToggleTags[i].style.visibility = "visible";
	   VicPopCal.arrToggleTags.length=0;
	}
    closeCalendar('on');
}

function HiddenDiv2(){	
	if(blnFlag){
  	   var i;
       VicPopCal.style.visibility = "hidden";
       if(VicPopCal.arrToggleTags){
          for (i=0;VicPopCal.arrToggleTags[i];i++) VicPopCal.arrToggleTags[i].style.visibility = "visible";
	      VicPopCal.arrToggleTags.length=0;
	   }
       closeCalendar('on');
  }
}

function fBuildCal(iYear, iMonth) {
  var aMonth=new Array();
  for(i=1;i<7;i++) aMonth[i]=new Array(i);
  
  var dCalDate=new Date(iYear, iMonth-1, 1);
  var iDayOfFirst=dCalDate.getDay();
  var iDaysInMonth=new Date(iYear, iMonth, 0).getDate();
  var iOffsetLast=new Date(iYear, iMonth-1, 0).getDate()-iDayOfFirst+1;
  var iDate = 1;
  var iNext = 1;

  for (d = 0; d < 7; d++) aMonth[1][d] = (d<iDayOfFirst)?-(iOffsetLast+d):iDate++;
  for (w = 2; w < 7; w++)
  	for (d = 0; d < 7; d++)
		aMonth[w][d] = (iDate<=iDaysInMonth)?iDate++:-(iNext++);
  return aMonth;
}

function tdmouseover(){
gcBG=this.bgColor;this.bgColor=gcToggle;
}
function tdmouseout(){
this.bgColor=gcBG;
}
var strError = ['该日期不能添加！','只可统计07-1-1以后的日期！','您不能选择今天以后的日期！'];
var numError = 0;
function tdonclickerror(numError){
	if(typeof numError=='number'&&strError[numError]) fnShowError(strError[numError]);
	if(typeof numError=='string') fnShowError(numError);
	return false;
}

function fDrawCal(iYear, iMonth, iCellHeight, sDateTextSize) {
  var WeekDay = new Array("日","一","二","三","四","五","六");
  var styleTD = "valign='middle' align='center' height='"+iCellHeight+"' style='font:bold arial "+sDateTextSize+";";   //padding-top:4px;padding-bottom:3px;        

  with (document) {
	write("<tr>");
	for(i=0; i<7; i++){
		write("<td align='center'>"+ WeekDay[i] + "</td>");
	}
	write("</tr>");

  	for (w = 1; w < 7; w++) {
		write("<tr>");
		for (d = 0; d < 7; d++) {
			write("<td id=calCell bgcolor='#ffffff' "+styleTD+"cursor:pointer;' onMouseOver='gcBG=this.bgColor;this.bgColor=gcToggle' onMouseOut='this.bgColor=gcBG' onclick='fSetSelected(this)'>");
			write("<a id=cellText flagMonth='' Victor='Liming Weng' onclick='return false;'> </a>");
			//write("<font id=cellText Victor='Liming Weng'> </font>");			
			write("</td>")
		}
		write("</tr>");
	}
  }
}

function fUpdateCal(iYear, iMonth) {
  myMonth = fBuildCal(iYear, iMonth);
  var num = 0;
  var tempY,tempM,tempD;
  var flag = true;
    
    //added by weng,for NS
    var objFont = document.getElementsByTagName('a');
    var cellText = new Array();
    for(var i=0;i<objFont.length;i++) if(objFont[i].getAttribute('id')=='cellText') cellText[cellText.length] = objFont[i];
    
    for (w = 0; w < 6; w++)
  	for (d = 0; d < 7; d++)
  		with (cellText[(7*w)+d]) {
			
			if(myMonth[w+1][d]<0){
			    if(w<2){tempM = (iMonth*1-1<1)?12:iMonth*1-1;if(tempM==12) tempY = iYear*1-1;else tempY = iYear*1;}
				if(w>3){tempM = (iMonth*1+1>12)?1:iMonth*1+1;if(tempM==1) tempY = iYear*1+1;else tempY = iYear*1;}
				tempD = -myMonth[w+1][d];
			}
			else {tempY = iYear;tempM = iMonth;tempD = myMonth[w+1][d]}
			intDate = tempY*10000+tempM*100+tempD*1;
			
			if(tempY<minY || tempY>maxY){
				numError = 0;
				parentNode.onmouseover = null;
			    parentNode.onmouseout = null;
				parentNode.onclick = new Function('tdonclickerror('+numError+')');
				parentNode.style.cursor = 'default';
				style.cursor = 'default';
				parentNode.bgColor='#f0f0f0';
				flag = false;
			}
			else if((beginning>0 && intDate<beginning) || (deadline>0 && intDate>deadline)){
				if(intDate<=beginning) numError = '\'您不能选择早于'+beginning.toString().substr(2,2)+'-'+beginning.toString().substr(4,2)*1+'-'+beginning.toString().substr(6,2)*1+'的日期\'';
				else if(intDate>deadline) numError = '\'您不能选择晚于'+deadline.toString().substr(2,2)+'-'+deadline.toString().substr(4,2)*1+'-'+deadline.toString().substr(6,2)*1+'的日期\'';
				
				parentNode.onmouseover = null;
			    parentNode.onmouseout = null;
				parentNode.onclick = new Function('tdonclickerror('+numError+')');
				parentNode.style.cursor = 'default';
				style.cursor = 'default';
				parentNode.bgColor='#f0f0f0';
				flag = false;
			}
			else {
			    parentNode.onmouseover = tdmouseover;
				parentNode.onmouseout = tdmouseout;
				parentNode.onclick = new Function('fSetSelected(this);return false;');
				parentNode.style.cursor = 'pointer';
				style.cursor = 'pointer';
				parentNode.bgColor='#ffffff';
				flag = true;
			}
			
  			setAttribute('Victor',num++);
  			if (myMonth[w+1][d]<0) {
  				setAttribute('flagMonth',0);
  				style.color = gcGray;
  				innerHTML = -myMonth[w+1][d];
  				
  				if(flag) parentNode.bgColor='#ffffff';//added by weng,2005.9.29
  			}else{
  				setAttribute('flagMonth',1);
  				style.color = ((d==0)||(d==6))?"#ff6600":"#333333";
  				innerHTML = myMonth[w+1][d];
  				
  				if(flagInitDate && iYear==giYear && iMonth==giMonth && myMonth[w+1][d]==giDay) parentNode.bgColor='#CFDBF1'; //标示当前选中日期
  				else if(flag) parentNode.bgColor='#ffffff';//added by weng,2005.9.29
				if(iYear==cYear && iMonth==cMonth && myMonth[w+1][d]==cDay) parentNode.style.fontWeight='bold';//.bgColor='#CFDBF1'; //标示today的bgColor
				else parentNode.style.fontWeight='normal';
  			}

  		}
		
		if(iYear==2000&&iMonth==1) document.getElementById('PrevMonth').disabled = true;
  		else document.getElementById('PrevMonth').disabled = false;
		if(iYear==2011&&iMonth==12) document.getElementById('NextMonth').disabled = true;
 		else document.getElementById('NextMonth').disabled = false;
}

function fSetYearMon(iYear, iMon){
  /*tbSelMonth.options[iMon-1].selected = true;
  for (i = 0; i < tbSelYear.length; i++)
	if (tbSelYear.options[i].value == iYear)
		tbSelYear.options[i].selected = true;*/
  tbSelYear.value=iYear;
  tbSelMonth.value=iMon;
  fUpdateCal(iYear, iMon);
}

function fPrevMonth(){
  var iMon = tbSelMonth.value;
  var iYear = tbSelYear.value;
  
  if (--iMon<1) {
	  iMon = 12;
	  iYear--;
  }
  
  fSetYearMon(iYear, iMon);
  
  if(iYear==2000&&iMon==1) document.getElementById('PrevMonth').disabled = true;
  else document.getElementById('PrevMonth').disabled = false;
}

function fNextMonth(){
  var iMon = tbSelMonth.value;
  var iYear = tbSelYear.value;
  
  if (++iMon>12) {
	  iMon = 1;
	  iYear++;
  }
  
  fSetYearMon(iYear, iMon);
  
  if(iYear==2011&&iMon==12) document.getElementById('NextMonth').disabled = true;
  else document.getElementById('NextMonth').disabled = false;
}

// Main: popCtrl is the widget beyond which you want this calendar to appear;
//       dateCtrl is the widget into which you want to put the selected date.
//       if(flagReventCal=1) display“清空”link
//       fnName:fSetDate()后执行的function
//       bDate:可以添加(fSetDate())的时间范围的开始时间    dDate:可以添加(fSetDate())的时间范围的结束时间
//       strDate:默认日期
// i.e.: <input type="text" name="dc" style="text-align:center" readonly><INPUT type="button" value="V" onclick="fPopCalendar(dc,dc);return false">
var beginning = 0;
var deadline = 0;
var flagInitDate = false;
function fPopCalendar(popCtrl, dateCtrl, flagReventCal, fnName, bDate, dDate, strDate){
  if(flagReventCal && document.getElementById('aReventCal')) document.getElementById('aReventCal').style.display='none';
  if(fnName) functionAfterSetDate=fnName;
  else functionAfterSetDate=null;
  if(bDate) {var arrBDate = bDate.toString().split('-');beginning = arrBDate[0]*10000+arrBDate[1]*100+arrBDate[2]*1;}
  else beginning = 0;
  if(dDate) {var arrDDate = dDate.toString().split('-');deadline = arrDDate[0]*10000+arrDDate[1]*100+arrDDate[2]*1;}
  else deadline = 0;
  
  if (popCtrl == previousObject){
	  	if (VicPopCal.style.visibility == "visible"){
  		HiddenDiv();
  		return true;    
  	}
  }
  
  previousObject = popCtrl;
  gdCtrl = dateCtrl;
  if(!strDate&&dateCtrl.value!='') strDate = dateCtrl.value;
  flagInitDate = fInitialDate(strDate);
  fSetYearMon(giYear, giMonth); 
  
  var aToday = document.getElementById('aToday');
  if(aToday && (beginning>0 || deadline>0)){
     var dateToday = cYear*10000+cMonth*100+cDay*1;
	 if(dateToday<=beginning || dateToday>deadline) aToday.onclick=new Function('tdonclickerror(0);return false;');
  }
  else if(aToday) aToday.onclick=new Function('fSetDate('+cYear+','+cMonth+','+cDay+');return false;');
  
  var point = fGetXY(popCtrl);
  VicPopCal.arrToggleTags = new Array();
  VicPopCal.exCon = 'getAttribute(\'Victor\')!=\'Won\'';
  hideShowError();
  displayLayer('VicPopCal',point.x,point.y+16,'Popup2');
  
  blnFlag = false;
}


function fInitialDate(strDate){
	giYear  = cYear;
	giMonth = cMonth;
	giDay   = cDay;
	
	if( strDate == null || strDate.length != 10 )
		return false;

	var sYear  = strDate.substring(0,4);
	var sMonth = strDate.substring(5,7);
	var sDay   = strDate.substring(8,10);

	if( sMonth.charAt(0) == '0' ) { sMonth = sMonth.substring(1,2); }
	if( sDay.charAt(0)   == '0' ) { sDay   = sDay.substring(1,2);   }

	var nYear  = parseInt(sYear );
	var nMonth = parseInt(sMonth);
	var nDay   = parseInt(sDay  );
	
	if ( isNaN(nYear ) )	return false;
	if ( isNaN(nMonth) )	return false;
	if ( isNaN(nDay  ) )	return false;

	var arrMon = new Array(12);
	arrMon[ 0] = 31;	arrMon[ 1] = nYear % 4 == 0 ? 29:28;
	arrMon[ 2] = 31;	arrMon[ 3] = 30;
	arrMon[ 4] = 31;	arrMon[ 5] = 30;
	arrMon[ 6] = 31;	arrMon[ 7] = 31;
	arrMon[ 8] = 30;	arrMon[ 9] = 31;
	arrMon[10] = 30;	arrMon[11] = 31;

	if ( nYear  < 1900 || nYear > 2100 )			return false;
	if ( nMonth < 1 || nMonth > 12 )				return false;
	if ( nDay < 1 || nDay > arrMon[nMonth - 1] )	return false;

	giYear  = nYear;
	giMonth = nMonth;
	giDay   = nDay;
	return true;
}

var gMonths = new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");
var minY = 2000,maxY = 2011;
with (document) {
write("<Div id='VicPopCal' style='OVERFLOW:hidden;POSITION:absolute;VISIBILITY:hidden;border:0px ridge;z-index:100;' onmouseover='blnFlag=false;' onclick='blnFlag=false;'>");
write("<table id='TableCal' border='0' bgcolor='#ffffff' style='border:#596e8b 1px solid;'>");
write("<tr><td style='text-align:center;color:#ff0000;display:none;' id='calendarErrInfoCon'></td></tr>");
write("<TR>");
write("<td valign='middle' align='center'><input type='button' class='button' title='上一个月' name='PrevMonth' id='PrevMonth' value='<' style='border:0;background:none;height:18;width:20;FONT:bold;cursor:pointer;' onfocus='this.blur()' onClick='fPrevMonth()'>");
write("&nbsp;<SELECT name='tbSelYear' id='tbSelYear' style='width:80px;' onChange='fUpdateCal(tbSelYear.value, tbSelMonth.value)' Victor='Won'>");
for(i=minY;i<=maxY;i++)
	write("<OPTION value='"+i+"'>"+i+" 年</OPTION>");
write("</SELECT>");
write("&nbsp;<select name='tbSelMonth' id='tbSelMonth' style='width:80px;' onChange='fUpdateCal(tbSelYear.value, tbSelMonth.value)' Victor='Won'>");
for (i=0; i<12; i++)
	write("<option value='"+(i+1)+"'>"+gMonths[i]+"</option>");
write("</SELECT>");
write("&nbsp;<input type='button' class='button' title='下一个月' name='NextMonth' id='NextMonth' value='>' style='border:0;background:none;height:18;width:20;FONT:bold;cursor:pointer;' onfocus='this.blur()' onclick='fNextMonth()'>");
write("</td>");
write("</TR><TR>");
write("<td align='center' bgcolor='#CFDBF1' class='tb_border' style='padding:0px;'>");
write("<DIV style='padding:0px;'><table width='100%' border='0' cellspacing='1' cellpadding='0'>");
fDrawCal(giYear, giMonth, 20, '12');
write("</table></DIV>");
write("</td>");
write("</TR><TR><TD align='center'>");
write("<TABLE width='100%'><TR><!--<TD>");
write("<a href='#' id='aToday' style='text-decoration:none' onclick='fSetDate(giYear,giMonth,giDay);return false;' onMouseOver='this.style.color=\"#000066\"' onMouseOut='this.style.color=\"#333333\"'><b>今天: "+giYear+"-"+giMonth+"-"+giDay+"</b></a>");
write("</td>--><td algin='right'>");
write("<a id='aReventCal' href='#' style='text-decoration:none' onclick='fSetDate(0,0,0);return false;' onMouseOver='this.style.color=\"#000066\"' onMouseOut='this.style.color=\"#333333\"'><b>清空</b></a>&nbsp;&nbsp;<a href='#' style='text-decoration:none' onclick='HiddenDiv();return false;' onMouseOver='this.style.color=\"#000066\"' onMouseOut='this.style.color=\"#333333\"'><b>关闭</b></a>");
write("</td></tr></table>");
write("</TD></TR>");
write("</TABLE></Div>");
}

function fnShowError(t){
	var con = document.getElementById('calendarErrInfoCon');
	if(con){
		con.innerHTML = t;
		if(con.style.display=='none') con.style.display = '';
	}
}
function hideShowError(){
	var con = document.getElementById('calendarErrInfoCon');
	if(con){
		con.innerHTML = '';
		con.style.display = 'none';
	}
}

var VicPopCal = document.getElementById('VicPopCal');
var tbSelMonth = document.getElementById('tbSelMonth');
var tbSelYear = document.getElementById('tbSelYear');


document.onclick = function(){
 HiddenDiv2();
 if(!blnFlag) blnFlag=!blnFlag;
}
document.onmouseover=function(){
 blnFlag=true;
}

function myAttachEvent(d,e,f){
	try {
		if(d.attachEvent) d.attachEvent("on"+e,f);
		else if(d.addEventListener) d.addEventListener(e,f,false);
		else{
			var oldF = eval('d.on'+e);
			if(typeof oldF!='function') eval('d.on'+e+'=f');
			else eval('d.on'+e)=function(){oldF();f();}
		}
	}
	catch (error){}
}
function myDetachEvent(d,e,f){
	try {
		if(d.detachEvent) d.detachEvent("on"+e,f);
		else if(d.removeEventListener) d.removeEventListener(e,f,false);
	}
	catch (error){}
}